package com.iretailer.report.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.model.CameraMall;
import com.iretailer.report.model.MallBusinessTime;
import com.iretailer.report.model.Malls;
import com.iretailer.report.service.BusinessTimeService;
import com.iretailer.report.service.CameraService;
import com.iretailer.report.service.MailService;
import com.iretailer.report.service.MallsService;
import com.iretailer.report.util.StringUtil;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by xjw on 2017/9/21.
 */
@Service
public class AlarmMail {
    //    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Logger log = LoggerFactory.getLogger(AlarmMail.class);
    @Autowired
    private CameraService cameraService;
    @Autowired
    private MallsService storeService;
    @Autowired
    private MailService mailService;
    @Autowired
    private BusinessTimeService businessTimeService;

    @Value("${deviceoutline.time:30}")
    private Integer outlineTime;

    private DateTimeFormatter tf = DateTimeFormatter.ofPattern("HH:mm:ss");
    private DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @SneakyThrows
    public void execute() {
        log.info("===进入==== 断线日志 === alarmMail");
        List<CameraMall> cameraMalls = cameraService.selectCameras(new QueryWrapper<>()
                .eq("Enabled", "1")
                .lt("LastDataTime", LocalDateTime.now().minusMinutes(outlineTime).format(dtf))
        );
        //查询是否有断线的设备
        Map<String, List<CameraMall>> cameraMallMap = cameraMalls.stream().collect(Collectors.groupingBy(CameraMall::getMallId));
        Set<String> mallIds = cameraMallMap.keySet();
        LocalDateTime now = LocalDateTime.now();
        String nowStr = now.format(dtf);
        for (String mallId : mallIds) {
            MallBusinessTime businessTime = businessTimeService.filterBusinessTime(mallId, now);
            LocalTime localTime = now.toLocalTime();
            if (businessTime == null ||
                    localTime.isBefore(LocalTime.parse(businessTime.getStartTime(), tf)) ||
                    localTime.isAfter(LocalTime.parse(businessTime.getEndTime(), tf))
            ) {
                continue;
            }
            Malls mall = storeService.getById(mallId);
            String principalMail = mall.getPrincipalMail();
            if (StringUtil.empty(principalMail)) {
                continue;
            }
            List<CameraMall> cameraMallList = cameraMallMap.get(mallId);
            //发送邮件报警
            log.info("==== 开始发送断线邮件 ===");
            String[] mails = principalMail.split(",");
            mailService.send(mails, "客流设备异常报警", new HashMap<String, Object>(4) {
                {
                    put("datetime", nowStr);
                    put("mallName", mall.getName());
                    put("data", cameraMallList);
                }
            }, "alarm-mail");
        }

    }
}

